package laowan;

import java.util.*;

/**
 * 图书管理类
 */
public class BookBiz {
    //声明图书的对象数组
    Book[] books = new Book[100];
    int index = 0;


    public BookBiz() {
        Book bk_1 = new Book();
        Book bk_2 = new Book();
        Book bk_3 = new Book();

        bk_1.count = 2;
        bk_2.count = 3;
        bk_3.count = 1;

        this.books[0] = bk_1;
        this.books[1] = bk_2;
        this.books[3] = bk_3;
    }
    /**
     * 新增图书
     * 参数：String类型 的图书名称
     * return 是否添加成功 true-成功 false-失败
     */
    public boolean addBook(String name) {
        //判断该书是否存在
        this.index = searchName(name);
        if (this.index == -1) {
            Book book = new Book();//创造空间赋值
            book.name = name;
            book.status = "未借";
            book.date = null;
            book.count = 0;

            for (int i = 0; i < this.books.length; i++) {
                if (this.books[i] == null) {//寻找空位
                    this.books[i] = book; //把对象赋给对象数组
                    return true;
                }
            }
            return false;//数组已经满了
        }else {
            return false;//图书存在
        }
    }

    /**
     * 删除图书
     */
    public boolean deleteBook(String name) {
        this.index = searchName(name);
        if (index != -1) {  //查询到图书
            if (this.books[index].status.equals("未借") && this.books[index].name.equals(name)) {
                this.books[index].name = null;
                return true;
            }
        }
        return false;//没有找到图书
    }

    /**
     * 借出图书
     * return true-借出成功 false-借出失败
     */
    public boolean  borrowBook(String name) {
        this.index = searchName(name);
        if (this.index != -1) {
            if (this.books[index].status.equals("未借")) {
                this.books[index].status = "借出";
                this.books[index].count += 1;
                return true;
            }
        }
        return false;
    }

    /**
     * 归还图书
     * return true-归还成功 false-归还失败
     */
    public boolean returnBook(String name) {
        this.index = searchName(name);
        if (this.index != -1) {
            if (this.books[index].status.equals("借出")) {
                this.books[index].status = "未借";
                return true;
            }
        }
        return false;
    }

    /**
     * 查询热度榜单
     *
     */
    public void heatBook() {
//        for (int i = 0; i < this.books.length; i++) {
//            for (int j = 0; j < this.books.length-i-1; j++) {
//                if (books[i].count < books[j].count) {
//                    int a = books[j].count;
//                    books[j].count = books[i].count;
//                    books[i].count = a;
//                    Arrays.sort(books);
//                }
//                System.out.println(books[j].count);
//            }
//        }
        Arrays.sort(books);
    }


    /**
     * 查找图书下标
     * 参数：String类型 图书名称
     * return 该书的下标 如果找不到返回 -1
     */
    public int searchName(String name) {
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null && books[i].name.equals(name)) {
                return i;
            }
        }
        return -1;
    }
}
